﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.AccessControl;

namespace PW_kalendarz_testy
{
    class Program
    {

        static void Main(string[] args)
        {
            //#region uwierzytelnianie
            //CalendarService myService = new CalendarService("pw-kalendarz-testy");
            //myService.setUserCredentials("kontotestowex@gmail.com", "haslotestx");
            //#endregion
            CalendarService service = new CalendarService("pw-kalendarz-testy");
            service.setUserCredentials("kontotestowex@gmail.com", "haslotest");

            //#region pobranie i wypisanie dostępnych kalendarzy
            //CalendarQuery query = new CalendarQuery();
            //query.Uri = new Uri("https://www.google.com/calendar/feeds/default/allcalendars/full");
            //CalendarFeed resultFeed = (CalendarFeed)myService.Query(query);
            //Console.WriteLine("Your calendars:\n");
            //Console.WriteLine(resultFeed.Feed);
            //foreach (CalendarEntry ent in resultFeed.Entries)
            //{
            //    Console.WriteLine(ent.Title.Text + "\n");
            //}
            //#endregion

            //#region dodanie wydarzenia do domyślnego kalendarza
            ////dodanie nowego wydarzenia do domyślnego kalendarza
            //Console.WriteLine("Dodaję nowe wydarzenie");
            //EventEntry entry = new EventEntry();

            //// Set the title and content of the entry.
            //entry.Title.Text = "jiaq";
            //entry.Content.Content = "pw";

            //// Set a location for the event.
            //Where eventLocation = new Where();
            //eventLocation.ValueString = "xxx";
            //entry.Locations.Add(eventLocation);

            //When eventTime = new When(DateTime.Now, DateTime.Now.AddHours(2));
            //entry.Times.Add(eventTime);

            //Uri postUri = new Uri("https://www.google.com/calendar/feeds/kontotestowex@gmail.com/private/full");

            //// Send the request and receive the response:
            //AtomEntry insertedEntry = service.Insert(postUri, entry);
            //#endregion

            //#region wypisanie wydarzeń w kalendarzu
            //Console.WriteLine("Wypisuję wydarzenia");

            //// Create the query object:
            //EventQuery myQuery = new EventQuery();
            //postUri = new Uri("https://www.google.com/calendar/feeds/default/private/basic");
            //myQuery.Uri = postUri;

            //// Tell the service to query:
            //EventFeed calFeed = myService.Query(myQuery);

            //foreach (var item in calFeed.Entries)
            //{
            //    Console.WriteLine(item.Title.Text);
            //}
            //#endregion
            //Console.ReadLine();

            //CalendarService service = new CalendarService("pw-kalendarz-testy");
            //service.setUserCredentials("kontotestowex@gmail.com", "haslotest");

            //FeedQuery query = new FeedQuery();
            //query.Uri = new Uri("https://www.google.com/calendar/feeds/default");
            //AtomFeed calFeed = service.Query(query);

            //// Retrieve the meta-feed of all calendars.
            //foreach (AtomEntry calendarEntry in calFeed.Entries)
            //{
            //    Console.WriteLine("Calendar: {0}", calendarEntry.Title.Text);
            //    AtomLink link = calendarEntry.Links.FindService(
            //        AclNameTable.LINK_REL_ACCESS_CONTROL_LIST, null);

            //    // For each calendar, retrieve its ACL feed.
            //    if (link != null)
            //    {
            //        AclFeed feed = service.Query(new AclQuery(link.HRef.ToString()));
            //        foreach (AclEntry aclEntry in feed.Entries)
            //        {
            //            Console.WriteLine("\tScope: Type={0} ({1})", aclEntry.Scope.Type,
            //                aclEntry.Scope.Value);
            //            Console.WriteLine("\tRole: {0}", aclEntry.Role.Value);
            //        }
            //    }
            //}

            //AclEntry entry = new AclEntry();

            //entry.Scope = new AclScope();
            //entry.Scope.Type = AclScope.SCOPE_USER;
            //entry.Scope.Value = "lordg90@gmail.com";

            //entry.Role = AclRole.ACL_CALENDAR_EDITOR;

            //Uri aclUri = new Uri("https://www.google.com/calendar/feeds/kontotestowex@gmail.com/acl/full");

            //AclEntry insertedEntry = service.Insert(aclUri, entry) as AclEntry;

            

            //EventQuery myQuery = new EventQuery("https://www.google.com/calendar/feeds/default/private/full");
            //myQuery.Query = "jiaq";
            //EventFeed myResultsFeed = service.Query(myQuery);
            ////if (myResultsFeed.Entries.Count > 0)
            ////{
            ////    AtomEntry firstMatchEntry = myResultsFeed.Entries[0];
            ////    String myEntryTitle = firstMatchEntry.Title.Text;
            ////    Console.WriteLine(myEntryTitle);
            ////    Console.WriteLine(firstMatchEntry.Authors.ElementAt(0).Email);
            ////}
            //Console.WriteLine("Zaczynam:");
            //foreach (var item in myResultsFeed.Entries)
            //{
            //    EventEntry x = (EventEntry)item;

            //    var whens = x.Times;
            //    var startTime = whens[0].StartTime;
            //    var endTime = whens[0].EndTime;

            //    Console.WriteLine(x.Title.Text);
            //    Console.WriteLine(x.Authors.ElementAt(0).Email);
            //    Console.WriteLine(startTime);
            //    Console.WriteLine(endTime);

   
            //}

            EventQuery myQuery = new EventQuery("https://www.google.com/calendar/feeds/default/private/full");
            myQuery.Query = "xxx";
            EventFeed myResultsFeed = service.Query(myQuery);
            foreach (var item in myResultsFeed.Entries)
            {
                EventEntry x = (EventEntry)item;

                var whens = x.Times;
                var startTime = whens[0].StartTime;
                var endTime = whens[0].EndTime;

                Console.WriteLine(x.Title.Text);
                Console.WriteLine(x.Authors.ElementAt(0).Email);
                Console.WriteLine("{0}{1}{2}", startTime.Year, startTime.Month, startTime.Day);
                Console.WriteLine("{0}{1}", endTime.Hour, startTime.Minute);
                int date = Convert.ToInt32(startTime.Year.ToString() + startTime.Month.ToString() + startTime.Day.ToString());
                Console.WriteLine("Moja data: {0}", date);
                int start = Convert.ToInt32(startTime.Hour.ToString() + startTime.Minute.ToString());
                Console.WriteLine("Moja godzina: {0}", start);
                Console.WriteLine(endTime);


            }

            Console.ReadLine();
        }
    }
}
